#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
//void gotoxy(int xx, int yy)
//{
//	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
//	COORD pos;
//	pos.X = xx;
//	pos.Y = yy;
//	SetConsoleCursorPosition(handle, pos);
//}

int move_y(char q,int y)
{
	if (q == 'w')
		y--;
	else if (q == 's')
		y++;
	return  y;
}
int move_x(char q, int x)
{
	if (q == 'a')
		x--;
	else if (q == 'd')
		x++;
	return x;
}
void PPrint(int x, int y,int xx,int yy,int point)
{
	int m, i;
	//gotoxy(0, 0);
	system("cls");
	for (i = 0; i < yy; i++)
		printf("\n");
	for (m = 1; m < xx; m++)
		printf(" ");
	printf("  X\n");

	for (i = yy; i < y; i++)
		printf("\n");
	for (m = 1; m < x; m++)
		printf(" ");
	printf("  *\n");
	for (m = 1; m < x; m++)
		printf(" ");
	printf("*****\n");
	for (m = 1; m < x; m++)
		printf(" ");
	printf(" * *\n");
	printf("point=%d", point);
}
void fire(int x, int y,int bx,int by,int xx,int yy,int point)
{
	int i, m;
	//gotoxy(0, 0);
	system("cls");
	for (i = 0; i < yy; i++)
		printf("\n");
	for (m = 1; m < xx; m++)
	    printf(" ");
	printf("  X\n");

	for (i = yy; i < by; i++)
		printf("\n");
	for (m = 1; m < bx; m++)
		printf(" ");
	printf("  I\n");

	for(i=by;i<y;i++)
		printf("\n");
	for (m = 1; m <= x; m++)
		printf(" ");
	printf(" *\n");
	for (m = 1; m < x; m++)
		printf(" ");
	printf("*****\n");
	for (m = 1; m < x; m++)
		printf(" ");
	printf(" * *\n");
	printf("point:%d", point);
}

int move_enemy(int toward,int xx)
{
	if (toward == 1)
		xx++;
	else if (toward == -1)
		xx--;
	return xx;
}

int way(int toward, int xx)
{
	if (xx <= 0)
		toward = 1;
	else if (xx >= 50)
		toward = -1;
	return toward;
}

int main()
{
	int point = 0;
	int x = 60, y = 40;
	int xx = 10, yy = 10,toward=1;//enemy
	int bx, by, s=0;//Memorize the bullet
	char input;
	while (1)
	{
		if (s == 0)
			PPrint(x, y,xx,yy,point);
		else
			fire(x, y,bx,by,xx,yy,point);
		xx = move_enemy(toward, xx);
		toward = way(toward, xx);
		input = _getch();
		y = move_y(input, y);
		x = move_x(input, x);
		if (s == 1)
		{
			by -= 1;
			if (bx==xx&& yy == by)
			{
				s = 0;
				point++;
			}
			if (by == yy-1)
				s = 0;
		}
		if (input == ' ')
		{
			if (s == 1)
				continue;
			s = 1;
			by = y - 1;
			bx = x;
		}
	}
	return 0;
}